var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var data = require('./static/data');
var mwRoute = require('./middlewares/mw_route');
var mwLogin = require('./middlewares/mw_login');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use("/", express.static(path.join(__dirname, "static")));

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(mwRoute(data));

app.get("/", function(req, resp){
    resp.redirect("/login");
});

app.get("/login(.html)?", function(req, resp){
    resp.render("login", {title: '登录页面'});
});

app.post("/login", mwLogin(data));

app.get("/index.html", function(req, resp){
    resp.render("index",data.indexPageInfo);
});

app.get("/usermanger.html", function(req, resp){
    resp.send("您好，您访问的是/usermanger.html页面");
});

app.get("/singleinfo.html", function(req, resp){
    resp.send("您好，您访问的是/singleinfo.html页面");
});

app.get("/leadermanger.html", function(req, resp){
    resp.send("您好，您访问的是/leadermanger.html页面");
});

app.get("/headmanger.html", function(req, resp){
    resp.send("您好，您访问的是/headmanger.html页面");
});

app.use(function(err, req, resp, next){
    if(err) {
        console.log(err.message);
    }
    resp.send("对不起,"+err.message);
});

app.listen(3000, function(){
    console.log("Server is running at localhost:3000.");
});
